package com.yunyao.common.core;

import com.alibaba.fastjson2.JSON;
import com.yunyao.common.web.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author xingjishuai
 * @date 2024/8/1
 */
public class BaseContext {

    private static final Logger log = LoggerFactory.getLogger(BaseContext.class);
    public static ThreadLocal<String> userIdthreadLocal = new ThreadLocal<>();
    public static ThreadLocal<UserInfoRequest> userInfothreadLocal = new ThreadLocal<>();

    public static String getUserId() {
        return userIdthreadLocal.get();
    }

    public static void setUserId(String id) {
        userIdthreadLocal.set(id);
    }

    public static void removeUserId() {
        userIdthreadLocal.remove();
    }
    public static UserInfoRequest getUserInfo() {
        return userInfothreadLocal.get();
    }

    public static List<String> getProjectList() {
        if (userInfothreadLocal.get() == null) {
            return null;
        }
        return userInfothreadLocal.get().getProjectIds();
    }


    public static List<String> getSupplierList() {
        if (userInfothreadLocal.get() == null) {
            return null;
        }
        return userInfothreadLocal.get().getSupplierIds();
    }

    public static void setUserInfo(String info) {
        if (StringUtils.isEmpty(info)) {
            return;
        }
        try {
            UserInfoRequest userInfoRequest = JSON.parseObject(info, UserInfoRequest.class);
            userInfothreadLocal.set(userInfoRequest);
        } catch (Exception exception) {
            log.error("用户信息格式异常", exception);
        }
    }

    public static void removeUserInfo() {
        userInfothreadLocal.remove();
    }

}
